//
//  RCAddMerchantViewController.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-10-1.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCAddMerchantViewController.h"
#import "NSArray+EX.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import "NSString+NSString_Ex_.h"
#import "GloableEngineService.h"
#import "NCCameraManager.h"
#import "UIImageView+CutToResize.h"
#import "RCSearchMoreTagsVC.h"
#import "AppDelegate.h"
#import "MBProgressHUD.h"
#import "IManager+ModuleAddMerchant.h"
#import "RCFeedbackDataModel.h"
#import "RCChoosePointMapViewController.h"

#define kLineImageFile         @"image_more_addMerchant_line.png"
#define kRightButtonImageFile  @"image_addMercahnt_rightButton.png"
#define kTakePhotoImageFile    @"image_addMercahnt_takePhoto.png"
#define kFormImageFile         @"image_about_textBG.png"
#define kCellArrowImageFile    @"image_merchant_cell_arrow.png"
#define kVerticalImageFile     @"image_more_addMerchant_verticalLine.png"
#define kMapButtonImageFile    @"image_allCity_district.png"

#define kTagTextFiledName      101
#define kTagTextFiledChannel   102
#define kTagTextFiledAddress   103
#define kTagTextFiledNearby    104
#define kTagTextFiledPhone     105
#define kTagTextFiledWeek      106
#define kTagTextFiledTime      107
#define kTagTextFiledPeople    108

#define kGetPicFromAlbumString		          @"从相册中选取"
#define kGetPicFromCameraString		          @"拍照"
#define kDeletePicString                      @"删除照片"
#define kCancelString				          @"取消"

@interface RCAddMerchantViewController ()<UITextFieldDelegate,SearchMoreTagsDelegate,MBProgressHUDDelegate,IManagerDelegate,RCChoosePointDelegate>

@property(nonatomic,retain)UIImageView *formImageView;
@property(nonatomic,retain)UIButton *rightButton;
@property(nonatomic,retain)UITextField *nameTextFiled;
@property(nonatomic,retain)UITextField *channelTextFiled;
@property(nonatomic,retain)UITextField *addressTextFiled;
@property(nonatomic,retain)UITextField *nearbyTextFiled;
@property(nonatomic,retain)UITextField *phoneTextFiled;
@property(nonatomic,retain)UITextField *weekTextFiled;
@property(nonatomic,retain)UITextField *timeTextFiled;
@property(nonatomic,retain)UITextField *peopleTextFiled;

@property(nonatomic,retain)UIButton *addPhotoButton;

@property(nonatomic,retain)NCCameraManager *cameraManager;

@property(nonatomic,copy)UIImage *photoImage;
@property(nonatomic,retain)MBProgressHUDEx *progressHub;
@property(nonatomic,assign)int64_t requestID;

@end

@implementation RCAddMerchantViewController

@synthesize rightButton = _rightButton;
@synthesize nameTextFiled = _nameTextFiled;
@synthesize channelTextFiled = _channelTextFiled;
@synthesize addressTextFiled = _addressTextFiled;
@synthesize nearbyTextFiled = _nearbyTextFiled;
@synthesize phoneTextFiled = _phoneTextFiled;
@synthesize weekTextFiled = _weekTextFiled;
@synthesize timeTextFiled = _timeTextFiled;
@synthesize peopleTextFiled = _peopleTextFiled;

@synthesize addPhotoButton = _addPhotoButton;
@synthesize formImageView = _formImageView;
@synthesize cameraManager = _cameraManager;
@synthesize photoImage = _photoImage;
@synthesize progressHub = _progressHub;
@synthesize requestID = _requestID;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)dealloc{
    
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_requestID];
    self.progressHub.delegate = nil;
    self.progressHub = nil;
    self.rightButton = nil;
    self.nameTextFiled = nil;
    self.channelTextFiled = nil;
    self.addPhotoButton = nil;
    self.nearbyTextFiled = nil;
    self.phoneTextFiled = nil;
    self.weekTextFiled = nil;
    self.timeTextFiled = nil;
    self.peopleTextFiled = nil;
    
    self.addPhotoButton = nil;
    
    self.formImageView = nil;
    self.cameraManager.delegate = nil;
    self.cameraManager = nil;
    self.photoImage = nil;
    [super dealloc];
}

-(void)loadView{
    [super loadView];
    self.titleLabelText = @"添加商户";
    self.navigationItem.titleView = [self createTitleView];
    
    _rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 55, 44)];
    [_rightButton setImage:[UIImage imageNamed:kRightButtonImageFile]
                  forState:UIControlStateNormal];
    [_rightButton setImage:[UIImage imageNamed:kRightButtonImageFile]
                  forState:UIControlStateHighlighted];
    [_rightButton addTarget:self action:@selector(commitButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [self createRightBarButtonItemWithButton:_rightButton];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    UIImage *lineImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kLineImageFile ofType:nil]];
    
    _formImageView = [[UIImageView alloc]initWithFrame:CGRectMake(14, 20, 292, 390)];
    _formImageView.userInteractionEnabled = YES;
    UIImage *formImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kFormImageFile ofType:nil]];
    formImage = [formImage resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
    [_formImageView setImage:formImage];
    
    //添加名称
    UILabel *nameStarLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 14, 15, 17)];
    nameStarLabel.backgroundColor = [UIColor clearColor];
    nameStarLabel.textColor = [UIColor colorWithHex:0xffff8200];
    nameStarLabel.font = [UIFont fontWithName:kFontNameHelvetica size:16];
    nameStarLabel.text = @"*";
    [_formImageView addSubview:nameStarLabel];
    [nameStarLabel release];
    
    UILabel *nameWordLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 11, 50, 16)];
    nameWordLabel.backgroundColor = [UIColor clearColor];
    nameWordLabel.textColor = [UIColor colorWithHex:0xff505050];
    nameWordLabel.font = [UIFont fontWithName:kFontNameHelvetica size:16];
    nameWordLabel.text = @"名  称";
    [_formImageView addSubview:nameWordLabel];
    [nameWordLabel release];
    
    _nameTextFiled = [[UITextField alloc]initWithFrame:CGRectMake(77, 1, 200, 37)];
    _nameTextFiled.backgroundColor = [UIColor clearColor];
    _nameTextFiled.delegate = self;
    _nameTextFiled.textColor = [UIColor colorWithHex:0xff505050];
    _nameTextFiled.font = [UIFont fontWithName:kFontNameLightHeiti size:14];
    _nameTextFiled.textAlignment = UITextAlignmentLeft;
    _nameTextFiled.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _nameTextFiled.placeholder = @"请输入商户名称";
    [_nameTextFiled setReturnKeyType:UIReturnKeyDone];
    [_formImageView addSubview:_nameTextFiled];
    
    UIImageView *nameLineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(-13, 39, 318, 1)];
    nameLineImageView.backgroundColor = [UIColor clearColor];
    [nameLineImageView setImage:lineImage];
    [_formImageView addSubview:nameLineImageView];
    [nameLineImageView release];
    
    //添加图片
    
    _addPhotoButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 46, 84, 58)];
    _addPhotoButton.backgroundColor = [UIColor clearColor];
    [_addPhotoButton setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kTakePhotoImageFile ofType:nil]] forState:UIControlStateNormal];
    [_addPhotoButton addTarget:self action:@selector(addPhotoButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [_formImageView addSubview:_addPhotoButton];
    
    UIImageView *photoLineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(-13, 110, 318, 1)];
    photoLineImageView.backgroundColor = [UIColor clearColor];
    [photoLineImageView setImage:lineImage];
    [_formImageView addSubview:photoLineImageView];
    [photoLineImageView release];
    
    //添加频道
    
    UILabel *channelStarLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 125, 15, 17)];
    channelStarLabel.backgroundColor = [UIColor clearColor];
    channelStarLabel.textColor = [UIColor colorWithHex:0xffff8200];
    channelStarLabel.font = [UIFont fontWithName:kFontNameHelvetica size:16];
    channelStarLabel.text = @"*";
    [_formImageView addSubview:channelStarLabel];
    [channelStarLabel release];
    
    UILabel *channelWordLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 122, 50, 16)];
    channelWordLabel.backgroundColor = [UIColor clearColor];
    channelWordLabel.textColor = [UIColor colorWithHex:0xff505050];
    channelWordLabel.font = [UIFont fontWithName:kFontNameHelvetica size:16];
    channelWordLabel.text = @"频  道";
    [_formImageView addSubview:channelWordLabel];
    [channelWordLabel release];
    
    _channelTextFiled = [[UITextField alloc]initWithFrame:CGRectMake(77, 112, 200, 37)];
    _channelTextFiled.backgroundColor = [UIColor clearColor];
    _channelTextFiled.delegate = self;
    _channelTextFiled.textColor = [UIColor colorWithHex:0xff505050];
    _channelTextFiled.font = [UIFont fontWithName:kFontNameLightHeiti size:14];
    _channelTextFiled.textAlignment = UITextAlignmentLeft;
    _channelTextFiled.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _channelTextFiled.placeholder = @"请选择频道";
    [_channelTextFiled setReturnKeyType:UIReturnKeyDone];
    [_formImageView addSubview:_channelTextFiled];
    
    UIImageView *channelArrowImageView = [[UIImageView alloc]initWithFrame:CGRectMake(275, 126, 7, 10)];
    NSString *arrowPath = [[NSBundle mainBundle]pathForResource:kCellArrowImageFile ofType:nil];
    [channelArrowImageView setImage:[UIImage imageWithContentsOfFile:arrowPath]];
    [_formImageView addSubview:channelArrowImageView];
    [channelArrowImageView release];
    
    UIButton *channelButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 111, 292, 39)];
    channelButton.backgroundColor = [UIColor clearColor];
    [channelButton addTarget:self action:@selector(channelButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [_formImageView addSubview:channelButton];
    [channelButton release];
    
    UIImageView *channelLineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(-13, 150, 318, 1)];
    channelLineImageView.backgroundColor = [UIColor clearColor];
    [channelLineImageView setImage:lineImage];
    [_formImageView addSubview:channelLineImageView];
    [channelLineImageView release];
    
    
    //添加地址
    UILabel *addressStarLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 165, 15, 17)];
    addressStarLabel.backgroundColor = [UIColor clearColor];
    addressStarLabel.textColor = [UIColor colorWithHex:0xffff8200];
    addressStarLabel.font = [UIFont fontWithName:kFontNameHelvetica size:16];
    addressStarLabel.text = @"*";
    [_formImageView addSubview:addressStarLabel];
    [addressStarLabel release];
    
    UILabel *addressWordLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 162, 50, 16)];
    addressWordLabel.backgroundColor = [UIColor clearColor];
    addressWordLabel.textColor = [UIColor colorWithHex:0xff505050];
    addressWordLabel.font = [UIFont fontWithName:kFontNameHelvetica size:16];
    addressWordLabel.text = @"地  址";
    [_formImageView addSubview:addressWordLabel];
    [addressWordLabel release];
    
    _addressTextFiled = [[UITextField alloc]initWithFrame:CGRectMake(77, 151, 175, 37)];
    _addressTextFiled.backgroundColor = [UIColor clearColor];
    _addressTextFiled.delegate = self;
    _addressTextFiled.textColor = [UIColor colorWithHex:0xff505050];
    _addressTextFiled.font = [UIFont fontWithName:kFontNameLightHeiti size:14];
    _addressTextFiled.textAlignment = UITextAlignmentLeft;
    _addressTextFiled.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _addressTextFiled.placeholder = @"如：陕西北路1438号";
    [_addressTextFiled setReturnKeyType:UIReturnKeyDone];
    [_formImageView addSubview:_addressTextFiled];
    
    UIButton *mapButton = [[UIButton alloc]initWithFrame:CGRectMake(252, 151, 40, 37)];
    mapButton.backgroundColor = [UIColor clearColor];
    [mapButton setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kMapButtonImageFile ofType:nil]] forState:UIControlStateNormal];
    [mapButton setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kMapButtonImageFile ofType:nil]] forState:UIControlStateHighlighted];
    [mapButton addTarget:self action:@selector(mapButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [_formImageView addSubview:mapButton];
    [mapButton release];
//    CLLocationCoordinate2D cor = [GloableEngineService sharedInstance].userLocation;
//    if (cor.latitude !=0.0 && cor.longitude != 0.0) {
//        UILabel *latLabel = [[UILabel alloc]initWithFrame:CGRectMake(77, 178, 200, 10)];
//        latLabel.backgroundColor = [UIColor clearColor];
//        latLabel.textColor = [UIColor colorWithHex:0xff505050];
//        latLabel.font = [UIFont fontWithName:kFontNameHelvetica size:10];
//        latLabel.text = [NSString stringWithFormat:@"经度：%.2lf 纬度：%.2lf",cor.longitude,cor.latitude];
//        [_formImageView addSubview:latLabel];
//        [latLabel release];
//        
//    }
    
    UIImageView *addressLineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(-13, 190, 318, 1)];
    addressLineImageView.backgroundColor = [UIColor clearColor];
    [addressLineImageView setImage:lineImage];
    [_formImageView addSubview:addressLineImageView];
    [addressLineImageView release];
    
    //添加靠近
    
    UILabel *nearbyWordLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 202, 50, 16)];
    nearbyWordLabel.backgroundColor = [UIColor clearColor];
    nearbyWordLabel.textColor = [UIColor colorWithHex:0xff505050];
    nearbyWordLabel.font = [UIFont fontWithName:kFontNameHelvetica size:16];
    nearbyWordLabel.text = @"靠  近";
    [_formImageView addSubview:nearbyWordLabel];
    [nearbyWordLabel release];
    
    _nearbyTextFiled = [[UITextField alloc]initWithFrame:CGRectMake(77, 192, 200, 37)];
    _nearbyTextFiled.backgroundColor = [UIColor clearColor];
    _nearbyTextFiled.delegate = self;
    _nearbyTextFiled.textColor = [UIColor colorWithHex:0xff505050];
    _nearbyTextFiled.font = [UIFont fontWithName:kFontNameLightHeiti size:14];
    _nearbyTextFiled.textAlignment = UITextAlignmentLeft;
    _nearbyTextFiled.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _nearbyTextFiled.placeholder = @"如：长寿路";
    [_nearbyTextFiled setReturnKeyType:UIReturnKeyDone];
    [_formImageView addSubview:_nearbyTextFiled];
    
    UIImageView *nearbyLineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(-13, 230, 318, 1)];
    nearbyLineImageView.backgroundColor = [UIColor clearColor];
    [nearbyLineImageView setImage:lineImage];
    [_formImageView addSubview:nearbyLineImageView];
    [nearbyLineImageView release];
    
    //添加电话
    
    UILabel *phoneWordLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 242, 50, 16)];
    phoneWordLabel.backgroundColor = [UIColor clearColor];
    phoneWordLabel.textColor = [UIColor colorWithHex:0xff505050];
    phoneWordLabel.font = [UIFont fontWithName:kFontNameHelvetica size:16];
    phoneWordLabel.text = @"电  话";
    [_formImageView addSubview:phoneWordLabel];
    [phoneWordLabel release];
    
    _phoneTextFiled = [[UITextField alloc]initWithFrame:CGRectMake(77, 232, 200, 37)];
    _phoneTextFiled.backgroundColor = [UIColor clearColor];
    _phoneTextFiled.delegate = self;
    _phoneTextFiled.textColor = [UIColor colorWithHex:0xff505050];
    _phoneTextFiled.font = [UIFont fontWithName:kFontNameHelvetica size:14];
    _phoneTextFiled.textAlignment = UITextAlignmentLeft;
    _phoneTextFiled.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _phoneTextFiled.placeholder = @"88888888";
    [_phoneTextFiled setReturnKeyType:UIReturnKeyDone];
    [_formImageView addSubview:_phoneTextFiled];
    
    UIImageView *phoneLineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(-13, 270, 318, 1)];
    phoneLineImageView.backgroundColor = [UIColor clearColor];
    [phoneLineImageView setImage:lineImage];
    [_formImageView addSubview:phoneLineImageView];
    [phoneLineImageView release];
    
    //添加营业时间
    UILabel *weekWordLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 282, 50, 16)];
    weekWordLabel.backgroundColor = [UIColor clearColor];
    weekWordLabel.textColor = [UIColor colorWithHex:0xff505050];
    weekWordLabel.font = [UIFont fontWithName:kFontNameHelvetica size:16];
    weekWordLabel.text = @"营  业";
    [_formImageView addSubview:weekWordLabel];
    [weekWordLabel release];
    
    UILabel *timeWordLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 303, 50, 16)];
    timeWordLabel.backgroundColor = [UIColor clearColor];
    timeWordLabel.textColor = [UIColor colorWithHex:0xff505050];
    timeWordLabel.font = [UIFont fontWithName:kFontNameHelvetica size:16];
    timeWordLabel.text = @"时  间";
    [_formImageView addSubview:timeWordLabel];
    [timeWordLabel release];
    
    UIImageView *timeVerticalImageView = [[UIImageView alloc]initWithFrame:CGRectMake(77, 271, 1, 80)];
    timeVerticalImageView.backgroundColor = [UIColor colorWithHex:0xffdddddd];
    [timeVerticalImageView setImage:[UIImage imageWithContentsOfFile:kVerticalImageFile]];
    [_formImageView addSubview:timeVerticalImageView];
    [timeVerticalImageView release];
    
    _weekTextFiled = [[UITextField alloc]initWithFrame:CGRectMake(87, 272, 200, 37)];
    _weekTextFiled.backgroundColor = [UIColor clearColor];
    _weekTextFiled.delegate = self;
    _weekTextFiled.textColor = [UIColor colorWithHex:0xff505050];
    _weekTextFiled.font = [UIFont fontWithName:kFontNameLightHeiti size:14];
    _weekTextFiled.textAlignment = UITextAlignmentLeft;
    _weekTextFiled.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _weekTextFiled.placeholder = @"周一至周五";
    [_weekTextFiled setReturnKeyType:UIReturnKeyDone];
    [_formImageView addSubview:_weekTextFiled];
    
    UIImageView *weekLineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(67, 310, 240, 1)];
    weekLineImageView.backgroundColor = [UIColor clearColor];
    [weekLineImageView setImage:lineImage];
    [_formImageView addSubview:weekLineImageView];
    [weekLineImageView release];
    
    _timeTextFiled = [[UITextField alloc]initWithFrame:CGRectMake(87, 312, 200, 37)];
    _timeTextFiled.backgroundColor = [UIColor clearColor];
    _timeTextFiled.delegate = self;
    _timeTextFiled.textColor = [UIColor colorWithHex:0xff505050];
    _timeTextFiled.font = [UIFont fontWithName:kFontNameHelvetica size:14];
    _timeTextFiled.textAlignment = UITextAlignmentLeft;
    _timeTextFiled.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _timeTextFiled.placeholder = @"08:00-24:00";
    [_timeTextFiled setReturnKeyType:UIReturnKeyDone];
    [_formImageView addSubview:_timeTextFiled];
    
    UIImageView *timeLineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(-13, 350, 318, 1)];
    timeLineImageView.backgroundColor = [UIColor clearColor];
    [timeLineImageView setImage:lineImage];
    [_formImageView addSubview:timeLineImageView];
    [timeLineImageView release];
    
    //添加电话
    
    UILabel *peopleWordLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 362, 50, 16)];
    peopleWordLabel.backgroundColor = [UIColor clearColor];
    peopleWordLabel.textColor = [UIColor colorWithHex:0xff505050];
    peopleWordLabel.font = [UIFont fontWithName:kFontNameHelvetica size:16];
    peopleWordLabel.text = @"经办人";
    [_formImageView addSubview:peopleWordLabel];
    [peopleWordLabel release];
    
    _peopleTextFiled = [[UITextField alloc]initWithFrame:CGRectMake(77, 352, 200, 37)];
    _peopleTextFiled.backgroundColor = [UIColor clearColor];
    _peopleTextFiled.delegate = self;
    _peopleTextFiled.textColor = [UIColor colorWithHex:0xff505050];
    _peopleTextFiled.font = [UIFont fontWithName:kFontNameHelvetica size:14];
    _peopleTextFiled.textAlignment = UITextAlignmentLeft;
    _peopleTextFiled.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _peopleTextFiled.placeholder = @"张三";
    [_peopleTextFiled setReturnKeyType:UIReturnKeyDone];
    [_formImageView addSubview:_peopleTextFiled];
    
    [self.viewContainer addSubview:_formImageView];
}

#pragma -mark UITextFeildDelegate

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([string isEqualToString:@"\n"]) {
        [textField resignFirstResponder];
        return NO;
    }
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    CGFloat keyboardOriginy = UIScreenHeight-216-40-64;
    CGFloat textFieldMaxy = CGRectGetMaxY(textField.frame)+20;
    if (textFieldMaxy > keyboardOriginy) {
        _formImageView.frame = CGRectMake(14, 20, 292, 390);
        [UIView animateWithDuration:0.5 animations:^{
            
            CGRect rect = _formImageView.frame;
            rect.origin.y = 21-(textFieldMaxy - keyboardOriginy);
            _formImageView.frame = rect;
        }];
        
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    [UIView animateWithDuration:0.5 animations:^{
        
        _formImageView.frame = CGRectMake(14, 20, 292, 390);
    }];
    
    
}

-(void)addPhotoButtonClicked{
    
    if (!_cameraManager){
        _cameraManager = [[NCCameraManager alloc] initWithParentController:self.navigationController isFromSelfView:NO];
        _cameraManager.delegate = (id<NCCameraManagerDelegate>)self;
    }
    
    UIActionSheet *actionSheet = nil;
    if ( [NCCameraManager supportGetPicFromAlbum] ) {
        if ( [NCCameraManager supportGetPicFromCamera] ) {
            if ( nil != self.photoImage) {
                actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:(id<UIActionSheetDelegate>)self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:kGetPicFromCameraString, kGetPicFromAlbumString,kDeletePicString, nil];
            }else{
                actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:(id<UIActionSheetDelegate>)self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:kGetPicFromCameraString, kGetPicFromAlbumString, nil];
            }
            
        } else {
            if (nil != self.photoImage) {
                actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:(id<UIActionSheetDelegate>)self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:kGetPicFromAlbumString,kDeletePicString, nil];
            }else{
                actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:(id<UIActionSheetDelegate>)self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:kGetPicFromAlbumString, nil];
            }
            
        }
    }
    

    if (actionSheet) {
        [actionSheet addButtonWithTitle:kCancelString];
        actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1;
        [actionSheet showFromTabBar:self.navigationController.tabBarController.tabBar];
        [actionSheet release];
    }
    
}

- (void)cancelCamera
{
    
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
	if (buttonIndex != [actionSheet cancelButtonIndex]) {
		NSString *titleString = [actionSheet buttonTitleAtIndex:buttonIndex];
		if ( NSOrderedSame == [titleString caseInsensitiveCompare:kGetPicFromAlbumString]) {
            
            
            [_cameraManager selectExistingPicture];
            
            
		} else if ( NSOrderedSame == [titleString caseInsensitiveCompare:kGetPicFromCameraString]) {
            
            [_cameraManager getCameraPictureFromAlbum:NO];
            
		}else if ( NSOrderedSame == [titleString caseInsensitiveCompare:kDeletePicString]) {
            
            self.photoImage = nil;
			[_addPhotoButton setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:kTakePhotoImageFile ofType:nil ]] forState:UIControlStateNormal];
		}
    }else {
        
    }
}

-(void)returnImage:(UIImage *)image{
    
    self.photoImage = image;
    
    UIImage *cutImage = [UIImageView imageCutToResize:image :CGSizeMake(84, 58)];
    [_addPhotoButton setImage:cutImage forState:UIControlStateNormal];
    if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 7.0){
        [self setNeedsStatusBarAppearanceUpdate];
    }
    
}

- (void)returnError:(NSString*)errString
{
    
}

-(void)channelButtonClicked{
    RCSearchMoreTagsVC *vc = [[RCSearchMoreTagsVC alloc]initWithParameters:[GloableEngineService sharedInstance].selectedCityID WithType:SearchMoreTagsVCTypeAddMerchant];
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

-(void)categoryDidSelect:(NSString *)categotyName{
    if (![NSString stringIsNullOrEmpty:categotyName]) {
        _channelTextFiled.text = categotyName;
    }
}


-(void)commitButtonClicked{
    if ([GloableEngineService sharedInstance].netWorkStatus == kNotReachable) {
        [RCAppDelegate alertWithNetworkError];
        return;
    }
    if ([NSString stringIsNullOrEmpty:_nameTextFiled.text]) {
        [RCAppDelegate alertWithCustomizeString:@"您还没有输入商户名称"];
        return;
    }
    if ([NSString stringIsNullOrEmpty:_channelTextFiled.text]) {
        [RCAppDelegate alertWithCustomizeString:@"您还没有选择频道"];
        return;
    }
    if ([NSString stringIsNullOrEmpty:_addressTextFiled.text]) {
        [RCAppDelegate alertWithCustomizeString:@"您还没有输入地址"];
        return;
    }
    
   
    _rightButton.enabled = NO;
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    [dict setObject:_nameTextFiled.text forKey:@"shopname"];
    [dict setObject:_channelTextFiled.text forKey:@"category"];
    [dict setObject:_addressTextFiled.text forKey:@"address"];
    [dict setObject:_nearbyTextFiled.text forKey:@"nearby"];
    [dict setObject:_phoneTextFiled.text forKey:@"phone"];
    [dict setObject:_weekTextFiled.text forKey:@"week"];
    [dict setObject:_timeTextFiled.text forKey:@"datetime"];
    [dict setObject:_peopleTextFiled.text forKey:@"name"];
    
    CLLocationCoordinate2D cor = [GloableEngineService sharedInstance].userLocation;
    
    [dict setObject:[NSString stringWithFormat:@"%lf",cor.latitude] forKey:@"lat"];
    [dict setObject:[NSString stringWithFormat:@"%lf",cor.longitude] forKey:@"lon"];
    
    if (self.photoImage != nil) {
        NSData *imageData = nil;
        if (nil == UIImagePNGRepresentation(self.photoImage)) {
            imageData = UIImageJPEGRepresentation(self.photoImage, .6);
        }else{
            imageData = UIImagePNGRepresentation(self.photoImage);
        }
        [dict setValue:imageData forKey:@"uploadfile"];
    }
    
    
    _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                               TopHeight:0
                                        withBottomHeight:0
                                           withLeftWidth:0
                                          withRightWidth:0
                                    withExclusiveObjects:nil];
    _progressHub.delegate = self;
    _progressHub.labelText = @"上传中...";
    [self.view addSubview:_progressHub];
    [self.view bringSubviewToFront:_progressHub];
    [_progressHub show:YES];
    _requestID = [[IManager sharedInstance]requestAddMerchantWithInfo:dict withDelegate:self];
}


-(void)updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId{
    if (_requestID == requestId){
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        RCFeedbackDataModel *feedbackDataModel = (RCFeedbackDataModel *)dataModel;
        if (feedbackDataModel.errorNO == 0) {
            [RCAppDelegate alertWithCustomizeString:@"提交成功"];
            _rightButton.enabled = NO;
            [self performSelector: @selector(leftButtonClicked) withObject: nil afterDelay: 1.5f];
        }else{
            [RCAppDelegate alertWithCustomizeString:@"提交失败，请稍后重试"];
            _rightButton.enabled = YES;
        }
    }
}

-(void)updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId{
    if (_requestID == requestId){
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        [RCAppDelegate alertWithCustomizeString:@"提交失败，请稍后重试"];
        _rightButton.enabled = YES;
        
    }
}

-(void)leftButtonClicked{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)mapButtonClicked{
    CLLocationCoordinate2D currentLocation = [GloableEngineService sharedInstance].userLocation;
    RCChoosePointMapViewController *mapView = [[RCChoosePointMapViewController alloc]initWithCoor:currentLocation andName:@"我的位置"];
    mapView.delegate = self;
    [self.navigationController pushViewController:mapView animated:YES];
    [mapView release];
}

-(void)chooseMerchantAddress:(NSString *)address{
    if (![NSString stringIsNullOrEmpty:address]) {
        _addressTextFiled.text = address;
    }
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
